package com.example.iread.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.iread.R;
import com.example.iread.adapter.BookInfoPagerAdapter;
import com.example.iread.application.MyApplication;
import com.example.iread.bean.BookBean;
import com.example.iread.dao.BookDao;
import com.example.iread.exception.MyException;
import com.example.iread.fragment.BookChapterFragment;
import com.example.iread.fragment.BookDescFragment;
import com.example.iread.http.BookApi;
import com.example.iread.util.GsonUtil;
import com.example.iread.util.HttpUtil;
import com.google.android.material.imageview.ShapeableImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class BookInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private final TextView[] tabs = new TextView[2];
    private ViewPager2 vp_book_info;

    private int bookId;
    private final String TAG = "SoRiya";
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_info);

        Intent intent = getIntent();
        bookId = intent.getIntExtra("bookId", 0);

        Log.i(TAG, "bookId: " + bookId);

        handler = new Handler(Looper.getMainLooper());

        initBaseData();

        // 加载ViewPager
        vp_book_info = findViewById(R.id.vp_book_info);


        // Tab
//        tabs[0] = findViewById(R.id.tv_book_info);
//        tabs[1] = findViewById(R.id.tv_book_chapter);
//        tabs[0].setOnClickListener(this);
//        tabs[1].setOnClickListener(this);

        // 默认选择第0个
//        selectTab(0);

        // 关闭按钮
        ImageView btn_book_info_close = findViewById(R.id.btn_book_info_close);
        btn_book_info_close.setOnClickListener(v -> {
            finish();
        });
    }

    @Override
    public void onClick(View view) {
        int index = 0;
        for (int i = 0; i < tabs.length; i++) {
            if (tabs[i] == view) {
                index = i;
                break;
            }
        }
        selectTab(index);
        vp_book_info.setCurrentItem(index);
    }

    public void selectTab(int index) {
        TextView view = tabs[index];

        // 排他
        for (TextView tv : tabs) {
            tv.setTextColor(getResources().getColor(R.color.grey, getTheme()));
            tv.getPaint().setFakeBoldText(false);
        }

        view.setTextColor(getResources().getColor(R.color.text_primary, getTheme()));
        view.getPaint().setFakeBoldText(true);
    }

    private void initBaseData() {
        TextView tv_book_name = findViewById(R.id.tv_book_name);
        TextView tv_book_author = findViewById(R.id.tv_book_author);
        ShapeableImageView img_book_cover = findViewById(R.id.img_book_cover);
        TextView tv_read_count = findViewById(R.id.tv_read_count);
        BookApi.getById(bookId, res -> {
            BookBean bookBean = GsonUtil.parseObject(res.data, BookBean.class);

            Log.i(TAG, "initBaseData: " + bookBean);

            BookDao bookDao = MyApplication.getInstance().getDb().bookDao();
            bookDao.deleteById(bookId);
            bookDao.insert(bookBean);

//            Log.i(TAG, "initBaseData: " + bookBean);
            handler.post(() -> {
                tv_book_name.setText(bookBean.bookName);
                tv_book_author.setText(bookBean.author);
                tv_read_count.setText(bookBean.read + " People");

                List<Fragment> fragments = new ArrayList<>();
//                fragments.add(new BookDescFragment(bookBean.bookInfo));
                fragments.add(new BookChapterFragment(bookBean.id));
                vp_book_info.setAdapter(new BookInfoPagerAdapter(this, fragments));
                vp_book_info.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                    @Override
                    public void onPageSelected(int position) {
//                        selectTab(position);
                    }
                });
            });
            new Thread(() -> {
                try {
                    InputStream inputStream = new URL(HttpUtil.getImageUrl(bookBean.cover)).openStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    handler.post(() -> img_book_cover.setImageBitmap(bitmap));
                } catch (IOException e) {
                    throw new MyException(500, e.getMessage());
                }
            }).start();
        });
    }
}